#!/usr/bin/env python3
"""
多样本位置检验 MCP 服务器启动脚本
"""

import sys
import os
import subprocess
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

def check_dependencies():
    """检查依赖包是否已安装"""
    required_packages = ['mcp', 'numpy', 'scipy']
    missing_packages = []
    
    for package in required_packages:
        try:
            __import__(package)
            logger.info(f"✓ {package} 已安装")
        except ImportError:
            missing_packages.append(package)
            logger.warning(f"✗ {package} 未安装")
    
    if missing_packages:
        logger.error(f"缺少依赖包: {', '.join(missing_packages)}")
        logger.info("请运行以下命令安装依赖:")
        logger.info("pip install -r requirements.txt")
        return False
    
    return True

def start_server():
    """启动MCP服务器"""
    logger.info("启动多样本位置检验 MCP 服务器...")
    
    # 检查依赖
    if not check_dependencies():
        sys.exit(1)
    
    # 获取当前脚本所在目录
    current_dir = os.path.dirname(os.path.abspath(__file__))
    server_path = os.path.join(current_dir, 'server.py')
    
    if not os.path.exists(server_path):
        logger.error(f"服务器文件不存在: {server_path}")
        sys.exit(1)
    
    try:
        # 启动服务器
        logger.info(f"运行服务器: {server_path}")
        subprocess.run([sys.executable, server_path], check=True)
    except KeyboardInterrupt:
        logger.info("服务器已停止")
    except subprocess.CalledProcessError as e:
        logger.error(f"服务器启动失败: {e}")
        sys.exit(1)
    except Exception as e:
        logger.error(f"未知错误: {e}")
        sys.exit(1)

if __name__ == "__main__":
    start_server()